package com.baby.publish;

import com.baby.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        //2.通道
        Channel channel = connection.createChannel();
        //创建队列
        channel.queueDeclare("publish_consumer02",true,false,false,null);
        //队列绑定交换机
        /*************************************************
         //String queue, String exchange, String routingKey
         第一个参数:queue:表示队列名称
         第二个参数:exchange:表示交换机名称
         第三个参数:routingKey:表示bindingkey
         ************************************************/
        channel.queueBind("publish_consumer02","pubsub_exchange","");
        //从队列种获取信息
        Consumer consumer =new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消息是02："+new String(body));
            }
        };

        channel.basicConsume("publish_consumer02",true,consumer);
    }
}
